package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.BlindDiskWorkBo;
import org.dromara.system.domain.vo.BlindDiskWorkVo;
import org.dromara.system.domain.BlindDiskWork;
import org.dromara.system.mapper.BlindDiskWorkMapper;
import org.dromara.system.service.IBlindDiskWorkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 盲板抽堵作业Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BlindDiskWorkServiceImpl implements IBlindDiskWorkService {

    private final BlindDiskWorkMapper baseMapper;

    /**
     * 查询盲板抽堵作业
     *
     * @param id 主键
     * @return 盲板抽堵作业
     */
    @Override
    public BlindDiskWorkVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询盲板抽堵作业列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 盲板抽堵作业分页列表
     */
    @Override
    public TableDataInfo<BlindDiskWorkVo> queryPageList(BlindDiskWorkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlindDiskWork> lqw = buildQueryWrapper(bo);
        Page<BlindDiskWorkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的盲板抽堵作业列表
     *
     * @param bo 查询条件
     * @return 盲板抽堵作业列表
     */
    @Override
    public List<BlindDiskWorkVo> queryList(BlindDiskWorkBo bo) {
        LambdaQueryWrapper<BlindDiskWork> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BlindDiskWork> buildQueryWrapper(BlindDiskWorkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlindDiskWork> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BlindDiskWork::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getZyzbh()), BlindDiskWork::getZyzbh, bo.getZyzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getSqdw()), BlindDiskWork::getSqdw, bo.getSqdw());
        lqw.eq(bo.getZysqsj() != null, BlindDiskWork::getZysqsj, bo.getZysqsj());
        lqw.eq(StringUtils.isNotBlank(bo.getSqr()), BlindDiskWork::getSqr, bo.getSqr());
        lqw.eq(StringUtils.isNotBlank(bo.getZynr()), BlindDiskWork::getZynr, bo.getZynr());
        lqw.eq(StringUtils.isNotBlank(bo.getZylb()), BlindDiskWork::getZylb, bo.getZylb());
        lqw.eq(StringUtils.isNotBlank(bo.getZywz()), BlindDiskWork::getZywz, bo.getZywz());
        lqw.eq(StringUtils.isNotBlank(bo.getGdjz()), BlindDiskWork::getGdjz, bo.getGdjz());
        lqw.eq(StringUtils.isNotBlank(bo.getGdwd()), BlindDiskWork::getGdwd, bo.getGdwd());
        lqw.eq(StringUtils.isNotBlank(bo.getGdyl()), BlindDiskWork::getGdyl, bo.getGdyl());
        lqw.eq(StringUtils.isNotBlank(bo.getMbcz()), BlindDiskWork::getMbcz, bo.getMbcz());
        lqw.eq(StringUtils.isNotBlank(bo.getMbgg()), BlindDiskWork::getMbgg, bo.getMbgg());
        lqw.eq(StringUtils.isNotBlank(bo.getZyryxx()), BlindDiskWork::getZyryxx, bo.getZyryxx());
        lqw.eq(StringUtils.isNotBlank(bo.getZydwlx()), BlindDiskWork::getZydwlx, bo.getZydwlx());
        lqw.eq(StringUtils.isNotBlank(bo.getZydwmc()), BlindDiskWork::getZydwmc, bo.getZydwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getZyfzr()), BlindDiskWork::getZyfzr, bo.getZyfzr());
        lqw.eq(bo.getZysskssj() != null, BlindDiskWork::getZysskssj, bo.getZysskssj());
        lqw.eq(bo.getZyssjssj() != null, BlindDiskWork::getZyssjssj, bo.getZyssjssj());
        lqw.eq(StringUtils.isNotBlank(bo.getMbwztbh()), BlindDiskWork::getMbwztbh, bo.getMbwztbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMbwzt()), BlindDiskWork::getMbwzt, bo.getMbwzt());
        lqw.eq(StringUtils.isNotBlank(bo.getBzrmc()), BlindDiskWork::getBzrmc, bo.getBzrmc());
        lqw.eq(bo.getBzsj() != null, BlindDiskWork::getBzsj, bo.getBzsj());
        lqw.eq(StringUtils.isNotBlank(bo.getSfsjqttszy()), BlindDiskWork::getSfsjqttszy, bo.getSfsjqttszy());
        lqw.eq(StringUtils.isNotBlank(bo.getSjqttszyzbh()), BlindDiskWork::getSjqttszyzbh, bo.getSjqttszyzbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFxbs()), BlindDiskWork::getFxbs, bo.getFxbs());
        lqw.eq(StringUtils.isNotBlank(bo.getAqjdr()), BlindDiskWork::getAqjdr, bo.getAqjdr());
        lqw.eq(StringUtils.isNotBlank(bo.getJsjdr()), BlindDiskWork::getJsjdr, bo.getJsjdr());
        lqw.eq(StringUtils.isNotBlank(bo.getJhr()), BlindDiskWork::getJhr, bo.getJhr());
        lqw.eq(StringUtils.isNotBlank(bo.getDqzt()), BlindDiskWork::getDqzt, bo.getDqzt());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), BlindDiskWork::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), BlindDiskWork::getLatitude, bo.getLatitude());
        lqw.eq(bo.getStartTime() != null, BlindDiskWork::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, BlindDiskWork::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getSpgbbm()), BlindDiskWork::getSpgbbm, bo.getSpgbbm());
        lqw.eq(StringUtils.isNotBlank(bo.getFhyp()), BlindDiskWork::getFhyp, bo.getFhyp());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachment()), BlindDiskWork::getAttachment, bo.getAttachment());
        return lqw;
    }

    /**
     * 新增盲板抽堵作业
     *
     * @param bo 盲板抽堵作业
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BlindDiskWorkBo bo) {
        BlindDiskWork add = MapstructUtils.convert(bo, BlindDiskWork.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改盲板抽堵作业
     *
     * @param bo 盲板抽堵作业
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BlindDiskWorkBo bo) {
        BlindDiskWork update = MapstructUtils.convert(bo, BlindDiskWork.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BlindDiskWork entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除盲板抽堵作业信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
